package com.wisemoresystem.seal.action.seal.news;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import org.ne.framework.data.util.ChkTools;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.wisemoresystem.seal.base.action.ProjBaseAction;
import com.wisemoresystem.seal.base.tip.Tip;
import com.wisemoresystem.seal.model.t_bank;
import com.wisemoresystem.seal.model.t_seal_new;
import com.wisemoresystem.seal.model.t_seal_type;
import com.wisemoresystem.seal.model.t_seal_type_config;
import com.wisemoresystem.seal.service.BankService;
import com.wisemoresystem.seal.service.SealNewService;
import com.wisemoresystem.seal.service.SealTypeConfigService;
import com.wisemoresystem.seal.web.WebTools;

/**
 * 新章申请
 * 
 * @author 冯晓东
 *
 */
@Controller("sealNewApply")
@Scope("prototype")
@RequestMapping("/ix/seal/news/apply")
public class SealNewsApplyAction extends ProjBaseAction {

	@Autowired
	private SealNewService sealNewService;
	@Autowired
	private SealTypeConfigService sealTypeService;
	@Autowired
	private BankService bService;

	/**
	 * 新章申请
	 * 
	 * @param srh_name
	 * @return
	 */
	@RequestMapping("/list")
	public ModelAndView list(String srh_name) {

		List<Map<String, Object>> list = sealNewService.selectSealNewList(
				srh_name, page);
		int lev = bService.getBankLevel(WebTools.getCurrentBankId());

		ModelAndView mv = new ModelAndView("/jsp/seal/news/apply/list");
		mv.addObject("list", list);
		mv.addObject("lev", lev);
		return mv;
	}

	/**
	 * 新章申请记录
	 * 
	 * @param srh_name
	 * @return
	 */
	@RequestMapping("/list_mine")
	public ModelAndView list_mine(String srh_name) {

		List<Map<String, Object>> list = sealNewService.selectSealNewList(
				srh_name, page);

		ModelAndView mv = new ModelAndView("/jsp/seal/news/apply/list_mine");
		mv.addObject("list", list);
		return mv;
	}

	@RequestMapping("/add_form")
	public ModelAndView add_form() {
		List<t_seal_type> sealTypeList = sealTypeService.selectAllTypeList();

		ModelAndView mv = new ModelAndView("/jsp/seal/news/apply/add_form");
		mv.addObject("sealTypeList", sealTypeList);
		return mv;
	}

	/**
	 * 新章申请添加
	 * 
	 * @param seal_new
	 *            新章信息
	 * @return
	 */
	@RequestMapping("/add")
	public ModelAndView add(t_seal_new seal_new) {

		if(ChkTools.isNotNull(seal_new.getTrue_code())){
			boolean islive = sealNewService.checkUniqueCode(seal_new.get_id(),
					seal_new.getTrue_code());
			if (!islive) {
				super.setTipMsg("该编码已存在，请重新输入！", Tip.Type.error);
				ModelAndView mv = new ModelAndView("/jsp/seal/news/apply/add_form");
				return mv;
			}
		}
		sealNewService.apply(seal_new);
		logT.logH("添加新章申请", t_seal_new.class, seal_new.get_id());

		if (seal_new.getFlag().equals("已保存")) {
			super.setTipMsg("保存成功", Tip.Type.success);
		} else {
			super.setTipMsg("进入复核", Tip.Type.success);
		}
		ModelAndView mv = new ModelAndView("redirect:list.htm");
		return mv;
	}

	/**
	 * 要修改的新章申请详细信息
	 * 
	 * @param seal_id
	 *            印章id
	 * @return
	 */
	@RequestMapping("/edit_form")
	public ModelAndView edit_form(String seal_id) {

		List<t_seal_type> sealTypeList = sealTypeService.selectAllTypeList();
		t_seal_new seal_new = sealNewService.findObjById(seal_id,
				t_seal_new.class);
		t_bank bank = sealNewService.findObjById(seal_new.getUse_bank_id(),
				t_bank.class);

		ModelAndView mv = new ModelAndView("/jsp/seal/news/apply/edit_form");
		mv.addObject("seal", seal_new);
		mv.addObject("bank", bank);
		if (ChkTools.isNotNull(seal_new.getSeal_type_config_id())) {
			t_seal_type_config config = sealNewService
					.findObjById(seal_new.getSeal_type_config_id(),
							t_seal_type_config.class);
			mv.addObject("config", config);
		}
		mv.addObject("sealTypeList", sealTypeList);
		return mv;
	}

	/**
	 * 新章申请修改
	 * 
	 * @param seal_id
	 *            印章id
	 * @param seal
	 *            印章信息
	 * @return
	 */
	@RequestMapping("/edit")
	public ModelAndView edit(String seal_id, t_seal_new seal) {

		boolean islive = sealNewService.checkUniqueCode(seal_id,
				seal.getTrue_code());
		if (!islive) {
			super.setTipMsg("该编码已存在，请重新输入！", Tip.Type.error);
			ModelAndView mv = new ModelAndView(
					"redirect:edit_form.htm?seal_id=" + seal_id);
			return mv;
		}
		t_seal_new seal_new = sealNewService.findObjById(seal_id,
				t_seal_new.class);
		seal_new.setUse_bank_id(seal.getBank_id());
		seal_new.setNote(seal.getNote());
		seal_new.setSeal_type_id(seal.getSeal_type_id());
		seal_new.setSeal_type_config_id(seal.getSeal_type_config_id());
		seal_new.setTitle(seal.getTitle());
		seal_new.setDesc_s(seal.getDesc_s());
		seal_new.setFlag(seal.getFlag());
		seal_new.setTrue_code(seal.getTrue_code());

		sealNewService.applyEdit(seal_new);
		logT.logH("修改新章申请", t_seal_new.class, seal_new.get_id());
		if (seal_new.getFlag().equals("已保存")) {
			super.setTipMsg("修改成功", Tip.Type.success);
		} else {
			super.setTipMsg("进入复核", Tip.Type.success);
		}
		ModelAndView mv = new ModelAndView("redirect:list.htm");
		return mv;
	}

	/**
	 * 新章申请删除
	 * 
	 * @param seal_id
	 *            印章id
	 * @return
	 */
	@RequestMapping("/del")
	public ModelAndView edit(String seal_id) {

		sealNewService.delObjById(seal_id, t_seal_new.class);
		logT.logH("删除新章申请", t_seal_new.class, seal_id);
		ModelAndView mv = new ModelAndView("redirect:list.htm");
		return mv;
	}

	@RequestMapping("/list_log")
	public ModelAndView mark_list(String srh_name) {
		List<Map<String, Object>> list = sealNewService
				.selectSealNewEngravingingLogList(WebTools.getCurrentUserId(),
						srh_name, page);

		ModelAndView mv = new ModelAndView("/jsp/seal/news/apply/list_log");
		mv.addObject("list", list);
		return mv;
	}

	@RequestMapping("/unique_code")
	public void unique_code(String seal_new_id, String true_code)
			throws IOException {

		boolean islive = sealNewService.checkUniqueCode(seal_new_id, true_code);

		resp.getWriter().print(islive);
		resp.getWriter().flush();
	}
}
